package com.yuanye.gwcit.fragments;

import android.content.Intent;

import androidx.annotation.Nullable;

import com.yuanye.gwcit.GW;
import com.yuanye.gwcit.activity.CameraActivity;
import com.yuanye.yeecho.base.CommonTaskFragment;
import com.yuanye.yeecho.utils.YY;

import java.util.Arrays;
import java.util.List;

public class CameraFragment extends CommonTaskFragment {

    String TAG = "CameraFragment";

    public CameraFragment() {
        setTaskName("摄像头");

    }

    @Override
    public void execTask() {
        checkCameraVidPid();
    }

    @Override
    public void stopTask() {

    }

    private void checkCameraVidPid(){
        String vid = YY.execShell("cat /sys/bus/usb/devices/1-1/idVendor");
        String pid = YY.execShell("cat /sys/bus/usb/devices/1-1/idProduct");
        if(vid.isEmpty() || pid.isEmpty()){
            appendLogF("未获取到camera ID");
            return;
        }
        String vidPid = vid + ":" + pid;
        appendLog("Camera ID：" +vidPid);
        String stdCameraId = GW.cfgPcba.getCameraId();
        appendLog("参考值："+stdCameraId);
        List<String> list = Arrays.asList(stdCameraId.split("#"));
        if(list.contains(vidPid)){
            goUVCCamera();
        }else{
            appendLogF("摄像头ID校验失败");
            setFail();
        }
    }

    private void goUVCCamera() {
        Intent intent = new Intent(getContext(), CameraActivity.class);
        startActivityForResult(intent, 100);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100){
            String info = "";
            if(data != null){
                info = data.getStringExtra("info");
            }
            if (resultCode == 200){
                if (!info.isEmpty())
                    appendLog(info);
                appendLog("判定通过");
                passAndNext();

            }else{
                if (!info.isEmpty())
                    appendLogF(info);
                appendLogF("判定失败");
                setFail();
            }
        }
    }

}
